/* 
 * Copyright (c) 2008 Knightsoft
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/
package com.knightsoft.util;

/**
 * 
 * @version V1.0 Nov 08, 2008
 * @author Michael
 */
public class Hex {
	public static String format(byte[] data) {
		StringBuilder result = new StringBuilder();
		int n = 0;
		for (byte b : data) {
			if (n % 16 == 0)
				result.append(String.format("%05X: ", n));
			result.append(String.format("%02X ", b));
			n++;
			if (n % 16 == 0)
				result.append("\n");
		}
		result.append("\n");
		return result.toString();
	}
	
	//Test
	/*
	public static void main(String[] args) throws Exception {
		if (args.length == 0)
			// Test by displaying this class file:
			System.out.println(format(BinaryFile.read("E:/eclipseWorkspace/KnightsoftClassLibrary/bin/com/knightsoft/util/Hex.class")));
		else
			System.out.println(format(BinaryFile.read(new File(args[0]))));
	}
	*/
}

/*
 * Output: (Hex.class) : 
00000: CA FE BA BE 00 00 00 32 00 61 07 00 02 01 00 17 
00010: 63 6F 6D 2F 6B 6E 69 67 68 74 73 6F 66 74 2F 75 
00020: 74 69 6C 2F 48 65 78 07 00 04 01 00 10 6A 61 76 
00030: 61 2F 6C 61 6E 67 2F 4F 62 6A 65 63 74 01 00 06 
00040: 3C 69 6E 69 74 3E 01 00 03 28 29 56 01 00 04 43 
00050: 6F 64 65 0A 00 03 00 09 0C 00 05 00 06 01 00 0F 
00060: 4C 69 6E 65 4E 75 6D 62 65 72 54 61 62 6C 65 01 
00070: 00 12 4C 6F 63 61 6C 56 61 72 69 61 62 6C 65 54 
00080: 61 62 6C 65 01 00 04 74 68 69 73 01 00 19 4C 63 
00090: 6F 6D 2F 6B 6E 69 67 68 74 73 6F 66 74 2F 75 74 
000A0: 69 6C 2F 48 65 78 3B 01 00 06 66 6F 72 6D 61 74 
000B0: 01 00 16 28 5B 42 29 4C 6A 61 76 61 2F 6C 61 6E 
000C0: 67 2F 53 74 72 69 6E 67 3B 07 00 11 01 00 17 6A 
000D0: 61 76 61 2F 6C 61 6E 67 2F 53 74 72 69 6E 67 42 
000E0: 75 69 6C 64 65 72 0A 00 10 00 09 08 00 14 01 00 
000F0: 06 25 30 35 58 3A 20 0A 00 16 00 18 07 00 17 01 
00100: 00 11 6A 61 76 61 2F 6C 61 6E 67 2F 49 6E 74 65 
00110: 67 65 72 0C 00 19 00 1A 01 00 07 76 61 6C 75 65 
00120: 4F 66 01 00 16 28 49 29 4C 6A 61 76 61 2F 6C 61 
00130: 6E 67 2F 49 6E 74 65 67 65 72 3B 0A 00 1C 00 1E 
00140: 07 00 1D 01 00 10 6A 61 76 61 2F 6C 61 6E 67 2F 
00150: 53 74 72 69 6E 67 0C 00 0E 00 1F 01 00 39 28 4C 
00160: 6A 61 76 61 2F 6C 61 6E 67 2F 53 74 72 69 6E 67 
00170: 3B 5B 4C 6A 61 76 61 2F 6C 61 6E 67 2F 4F 62 6A 
00180: 65 63 74 3B 29 4C 6A 61 76 61 2F 6C 61 6E 67 2F 
00190: 53 74 72 69 6E 67 3B 0A 00 10 00 21 0C 00 22 00 
001A0: 23 01 00 06 61 70 70 65 6E 64 01 00 2D 28 4C 6A 
001B0: 61 76 61 2F 6C 61 6E 67 2F 53 74 72 69 6E 67 3B 
001C0: 29 4C 6A 61 76 61 2F 6C 61 6E 67 2F 53 74 72 69 
001D0: 6E 67 42 75 69 6C 64 65 72 3B 08 00 25 01 00 05 
001E0: 25 30 32 58 20 0A 00 27 00 29 07 00 28 01 00 0E 
001F0: 6A 61 76 61 2F 6C 61 6E 67 2F 42 79 74 65 0C 00 
00200: 19 00 2A 01 00 13 28 42 29 4C 6A 61 76 61 2F 6C 
00210: 61 6E 67 2F 42 79 74 65 3B 08 00 2C 01 00 01 0A 
00220: 0A 00 10 00 2E 0C 00 2F 00 30 01 00 08 74 6F 53 
00230: 74 72 69 6E 67 01 00 14 28 29 4C 6A 61 76 61 2F 
00240: 6C 61 6E 67 2F 53 74 72 69 6E 67 3B 01 00 04 64 
00250: 61 74 61 01 00 02 5B 42 01 00 06 72 65 73 75 6C 
00260: 74 01 00 19 4C 6A 61 76 61 2F 6C 61 6E 67 2F 53 
00270: 74 72 69 6E 67 42 75 69 6C 64 65 72 3B 01 00 01 
00280: 6E 01 00 01 49 01 00 01 62 01 00 01 42 01 00 0D 
00290: 53 74 61 63 6B 4D 61 70 54 61 62 6C 65 07 00 32 
002A0: 01 00 04 6D 61 69 6E 01 00 16 28 5B 4C 6A 61 76 
002B0: 61 2F 6C 61 6E 67 2F 53 74 72 69 6E 67 3B 29 56 
002C0: 01 00 0A 45 78 63 65 70 74 69 6F 6E 73 07 00 3F 
002D0: 01 00 13 6A 61 76 61 2F 6C 61 6E 67 2F 45 78 63 
002E0: 65 70 74 69 6F 6E 09 00 41 00 43 07 00 42 01 00 
002F0: 10 6A 61 76 61 2F 6C 61 6E 67 2F 53 79 73 74 65 
00300: 6D 0C 00 44 00 45 01 00 03 6F 75 74 01 00 15 4C 
00310: 6A 61 76 61 2F 69 6F 2F 50 72 69 6E 74 53 74 72 
00320: 65 61 6D 3B 08 00 47 01 00 4C 45 3A 2F 65 63 6C 
00330: 69 70 73 65 57 6F 72 6B 73 70 61 63 65 2F 4B 6E 
00340: 69 67 68 74 73 6F 66 74 43 6C 61 73 73 4C 69 62 
00350: 72 61 72 79 2F 62 69 6E 2F 63 6F 6D 2F 6B 6E 69 
00360: 67 68 74 73 6F 66 74 2F 75 74 69 6C 2F 48 65 78 
00370: 2E 63 6C 61 73 73 0A 00 49 00 4B 07 00 4A 01 00 
00380: 1E 63 6F 6D 2F 6B 6E 69 67 68 74 73 6F 66 74 2F 
00390: 75 74 69 6C 2F 42 69 6E 61 72 79 46 69 6C 65 0C 
003A0: 00 4C 00 4D 01 00 04 72 65 61 64 01 00 16 28 4C 
003B0: 6A 61 76 61 2F 6C 61 6E 67 2F 53 74 72 69 6E 67 
003C0: 3B 29 5B 42 0A 00 01 00 4F 0C 00 0E 00 0F 0A 00 
003D0: 51 00 53 07 00 52 01 00 13 6A 61 76 61 2F 69 6F 
003E0: 2F 50 72 69 6E 74 53 74 72 65 61 6D 0C 00 54 00 
003F0: 55 01 00 07 70 72 69 6E 74 6C 6E 01 00 15 28 4C 
00400: 6A 61 76 61 2F 6C 61 6E 67 2F 53 74 72 69 6E 67 
00410: 3B 29 56 07 00 57 01 00 0C 6A 61 76 61 2F 69 6F 
00420: 2F 46 69 6C 65 0A 00 56 00 59 0C 00 05 00 55 0A 
00430: 00 49 00 5B 0C 00 4C 00 5C 01 00 12 28 4C 6A 61 
00440: 76 61 2F 69 6F 2F 46 69 6C 65 3B 29 5B 42 01 00 
00450: 04 61 72 67 73 01 00 13 5B 4C 6A 61 76 61 2F 6C 
00460: 61 6E 67 2F 53 74 72 69 6E 67 3B 01 00 0A 53 6F 
00470: 75 72 63 65 46 69 6C 65 01 00 08 48 65 78 2E 6A 
00480: 61 76 61 00 21 00 01 00 03 00 00 00 00 00 03 00 
00490: 01 00 05 00 06 00 01 00 07 00 00 00 2F 00 01 00 
004A0: 01 00 00 00 05 2A B7 00 08 B1 00 00 00 02 00 0A 
004B0: 00 00 00 06 00 01 00 00 00 19 00 0B 00 00 00 0C 
004C0: 00 01 00 00 00 05 00 0C 00 0D 00 00 00 09 00 0E 
004D0: 00 0F 00 01 00 07 00 00 01 2E 00 06 00 07 00 00 
004E0: 00 75 BB 00 10 59 B7 00 12 4C 03 3D 2A 59 3A 06 
004F0: BE 36 05 03 36 04 A7 00 4E 19 06 15 04 33 3E 1C 
00500: 10 10 70 9A 00 18 2B 12 13 04 BD 00 03 59 03 1C 
00510: B8 00 15 53 B8 00 1B B6 00 20 57 2B 12 24 04 BD 
00520: 00 03 59 03 1D B8 00 26 53 B8 00 1B B6 00 20 57 
00530: 84 02 01 1C 10 10 70 9A 00 0A 2B 12 2B B6 00 20 
00540: 57 84 04 01 15 04 15 05 A1 FF B1 2B 12 2B B6 00 
00550: 20 57 2B B6 00 2D B0 00 00 00 03 00 0A 00 00 00 
00560: 32 00 0C 00 00 00 1B 00 08 00 1C 00 0A 00 1D 00 
00570: 1D 00 1E 00 24 00 1F 00 39 00 20 00 4E 00 21 00 
00580: 51 00 22 00 58 00 23 00 5F 00 1D 00 69 00 25 00 
00590: 70 00 26 00 0B 00 00 00 2A 00 04 00 00 00 75 00 
005A0: 31 00 32 00 00 00 08 00 6D 00 33 00 34 00 01 00 
005B0: 0A 00 6B 00 35 00 36 00 02 00 1D 00 42 00 37 00 
005C0: 38 00 03 00 39 00 00 00 3F 00 04 FF 00 17 00 07 
005D0: 07 00 3A 07 00 10 01 00 01 01 07 00 3A 00 00 FF 
005E0: 00 21 00 07 07 00 3A 07 00 10 01 01 01 01 07 00 
005F0: 3A 00 00 FF 00 25 00 07 07 00 3A 07 00 10 01 00 
00600: 01 01 07 00 3A 00 00 02 00 09 00 3B 00 3C 00 02 
00610: 00 3D 00 00 00 04 00 01 00 3E 00 07 00 00 00 6D 
00620: 00 05 00 01 00 00 00 2D 2A BE 9A 00 14 B2 00 40 
00630: 12 46 B8 00 48 B8 00 4E B6 00 50 A7 00 19 B2 00 
00640: 40 BB 00 56 59 2A 03 32 B7 00 58 B8 00 5A B8 00 
00650: 4E B6 00 50 B1 00 00 00 03 00 0A 00 00 00 12 00 
00660: 04 00 00 00 2A 00 05 00 2C 00 16 00 2E 00 2C 00 
00670: 2F 00 0B 00 00 00 0C 00 01 00 00 00 2D 00 5D 00 
00680: 5E 00 00 00 39 00 00 00 04 00 02 16 15 00 01 00 
00690: 5F 00 00 00 02 00 60 
 */
